<template>
	<view class="denglbox">
		<view class="sishitext">立即注册</view>
		<view class="margin_top7">
			<input class="inputs" type="number" placeholder="请输入手机号码" v-model="mobile">
		</view>
		<view class="margin_top inputs flexbetween">
			<input class="kuandu" type="text" placeholder="请输入手机验证码" v-model="code">
			<view class="huangsetext" @click="sendcode">{{codetext}}</view>
		</view>
		<view class="margin_top">
			<input class="inputs" type="text" placeholder="请输入密码" v-model="passwordtext">
		</view>
		<view class="margin_top">
			<input class="inputs" type="text" placeholder="请再次输入密码" v-model="password">
		</view>
		<view class="margin_top">
			<input class="inputs" type="text" placeholder="请输入邀请码（选填）" v-model="invite_code">
		</view>
		<view class="margin_top flexleft" @click="xiaoshi">
			<view :class="quan?'yigouxuan margin_right1':'weigouxuan margin_right1'"></view>
			<view class="flexleft">
				<view class="huisetext">阅读并同意</view>
				<view class="huangsetext" @click="yizygy(1)">《用户协议》</view>
				<view class="huisetext">和</view>
				<view class="huangsetext" @click="yizygy(2)">《隐私保护政策》</view>
			</view>
		</view>
		<view class="margin_top bigbtn" @click="regist">立即注册</view>
		<view class="margin_top flexcenter">
			<view class="jiutext">已有账号，</view>
			<view class="huangsetext" @click="tzljdl">立即登录</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				quan: false,
				mobile: "",
				passwordtext:"",
				password: "",
				code: "",
				invite_code: "",
				codetext: "获取验证码",
			}
		},
		onLoad() {
			this.invite_code = uni.getStorageSync("yqm")
		},
		methods: {
			// 发送验证码
			sendcode() {
				let _this = this
				let mobile = _this.mobile;
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				httpRequest.request('/api/sms/send', 'GET', {
					mobile: mobile,
					event: "register"
				}).then(resp => {
					if (resp.code == 1) {
						var i = 60;
						var timer = setInterval(function() {
							_this.codetext = i + "秒";
							if (i < 1) {
								clearInterval(timer);
								_this.codetext = "获取验证码";
							} else {
								i--;
							}
						}, 1000)

					} else if (resp.code == 0) {
						httpRequest.toast(resp.msg);
					} else {
						httpRequest.toast('网络开小差啦');
					}
				})
			},
			// 注册
			regist() {
				let _this = this
				let mobile = _this.mobile;
				
				if (mobile == '') {
					httpRequest.toast('请输入手机号');
					return false;
				}
				if (!mobile.match(/^1\d{10}$/)) {
					httpRequest.toast("请输入有效的手机号码");
					return false;
				}
				if (_this.code == '') {
					httpRequest.toast("请输入验证码");
					return false;
				}
				if (_this.passwordtext == '') {
					httpRequest.toast("请输入密码");
					return false;
				}
				if (_this.password == '') {
					httpRequest.toast("请输入确认密码");
					return false;
				}
				if (_this.passwordtext != _this.password) {
					httpRequest.toast("两次输入密码不一致,请重新输入")
					return false
				}
				if(!this.quan){
					httpRequest.toast('请仔细阅读用户协议并勾选');
					return false;
				}
				httpRequest.request('/api/user/register', 'POST', {
					mobile: _this.mobile,
					password: _this.password,
					code: _this.code,
					invite_code:_this.invite_code
				}).then(res => {
					if (res.code == 1) {
						httpRequest.toast(res.msg);
						// 数据存token
						uni.setStorageSync('token', res['data']['userinfo']['token']);
						// 数据存用户id
						uni.setStorageSync('user_id', res['data']['userinfo']['id']);
						setTimeout(function() {
							uni.navigateBack({
								delta: 2
							});
						}, 2000)
					} else if (res.code == 0) {
						httpRequest.toast(res.msg);
					} else {
						httpRequest.toast('网络开小差啦');
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			// 跳转立即登录
			tzljdl() {
				uni.navigateTo({
					url: '/pages/login/zhanghmmdl'
				})
			},
			xiaoshi() {
				this.quan = !this.quan
			},
			// 跳转用户协议，隐私政策，关于我们
			yizygy(i){
				uni.navigateTo({
					url: '/pages/mypage/guanyuwomen?id='+i
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.kuandu {
		width: 60%;
	}

	.jiutext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.huangsetext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FAB536;
	}

	.huisetext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.ershib {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
	}

	.inputs {
		padding-bottom: 30rpx;
		border-bottom: 2rpx solid rgba(153, 153, 153, 0.1);
	}

	.sishitext {
		font-size: 40rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #000000;
	}

	.denglbox {
		padding: 208rpx 30rpx 0;
	}
</style>
